<!-- 库房库位 -->
<template>
  <div class="app-container">
    <div class="container">
      <div class="search-and-button">
        <el-button type="primary" icon="el-icon-plus" @click="showDialog(null)">新建</el-button>
        <el-button type icon="el-icon-refresh-left" @click="$router.push('/reload')">刷新</el-button>
        <el-button type icon="el-icon-more">更多</el-button>
      </div>
      <el-row class="table">
        <el-table ref="tableData" v-loading="loading" :element-loading-text="`加载中...`" :data="tableData" stripe border row-key="id" default-expand-all :tree-props="{ children: 'children', hasChildren: false }">
          <el-table-column type="selection" align="center"></el-table-column>
          <el-table-column align="center" type="index" label="索引" width="60px" />
          <el-table-column v-for="item in fieldList" :key="item.id" :align="item.align || 'left'" :prop="item.code.split(':')[1]" :label="item.val" :min-width="item.width + 'px'" />
          <el-table-column label="操作" fixed="right" align="center" width="100px">
            <template #default="{ row }">
              <el-button-group>
                <el-button type="text" size="medium" @click="showDialog(row)">
                  <i class="iconfont icon-search"></i>
                </el-button>
                <!-- <el-button v-if="!(row.children && row.children.length)" type="danger" icon="el-icon-delete" @click="deleteRow(row)" /> -->
              </el-button-group>
            </template>
          </el-table-column>
        </el-table>
      </el-row>
      <el-row>
        <div class="table-btn-box">
          <el-button @click="
            () => {
              tableData.forEach(row => {
                $refs['tableData'].toggleRowSelection(row, true)
                if (row.children && row.children.length > 0) {
                  row.children.forEach(child => {
                    $refs['tableData'].toggleRowSelection(child, true)
                  })
                }
              })
            }
          ">选择全部</el-button>
          <el-button @click="
            () => {
              tableData.forEach(row => {
                $refs['tableData'].toggleRowSelection(row)
                if (row.children && row.children.length > 0) {
                  row.children.forEach(child => {
                    $refs['tableData'].toggleRowSelection(child)
                  })
                }
              })
            }
          ">反向选择</el-button>
          <el-button>
            更多菜单
            <i class="el-icon-arrow-down el-icon--right"></i>
          </el-button>
        </div>
      </el-row>
      <!-- 新增编辑 -->
      <modify ref="addAndModify" @getTableInfo="getTableInfo" />
    </div>
  </div>
</template>
<script>
import modify from './components'
import { treeData } from '@/utils/function'
export default {
  components: { modify },
  data() {
    return {
      search: {},
      loading: false,
      tableData: []
    }
  },
  computed: {},
  mounted() {
    this.getTableInfo()
  },
  methods: {
    async getTableInfo() {
      this.loading = true
      const res = await this.$axios.get('/wms/list')
      if (res?.data.code === 200) {
        this.tableData = res.data.data
        setTimeout(() => {
          this.loading = false
        }, 100)
      }
    },
    /**
     * @description: 新增编辑
     * @return {*}
     */
    async showDialog(row) {
      const res = await this.$axios.get(
        `/wms/allFilterCode?id=${row?.id || ''}`
      )
      if (res?.data.code === 200) {
        this.$refs['addAndModify'].showDialog(
          row,
          treeData(res.data.data, 'id', 'parentId', 'children')
        )
      } else {
        this.$notify.error(res.data.msg)
      }
    }
  }
}
</script>
<style lang='scss' scoped>
.search-and-button {
  line-height: 30px;
  width: 100%;
  display: flex;
  justify-content: right;
}
.table {
  margin-top: 20px;
}
</style>
